﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YArchitech.Revit;
using YJKArchUtils.Utils;

namespace YJKRevitFloorModule.CreateFloor
{
	public class RoomCurveManager
	{
		public RoomCurveManager(Autodesk.Revit.DB.Document inputDoc)
		{
			this.m_Doc = inputDoc;
		}

		public List<CurveInformationBean> GetCurveFormRooms(List<Room> lstInputRooms, List<Curve> lstInputCurve)
		{
			List<CurveInformationBean> list = new List<CurveInformationBean>();
			if (lstInputCurve != null && lstInputCurve.Count != 0)
			{
				foreach (Curve inputCurve in lstInputCurve)
				{
					list.Add(new CurveInformationBean(inputCurve, null, null));
				}
			}
			if (lstInputRooms != null && lstInputRooms.Count != 0)
			{
				foreach (Room inputRoom in lstInputRooms)
				{
					list.AddRange(this.GetOneRoomCurve(inputRoom));
				}
			}
			return list;
		}

		private List<CurveInformationBean> GetOneRoomCurve(Room inputRoom)
		{
			List<CurveInformationBean> list = new List<CurveInformationBean>();
			IEnumerable<IList<BoundarySegment>> boundarySegments = inputRoom.GetBoundarySegments(new SpatialElementBoundaryOptions
			{
                SpatialElementBoundaryLocation = (SpatialElementBoundaryLocation)1
			});
			ElementId elementId = null;
			foreach (IList<BoundarySegment> list2 in boundarySegments)
			{
				IEnumerator<BoundarySegment> enumerator2 = list2.GetEnumerator();
				{
					while (enumerator2.MoveNext())
					{
						BoundarySegment boundarySegment = enumerator2.Current;
						elementId = null;
						Element element = null;
						RevitLinkInstance revitLinkInstance = null;
						ElementId boundarySegmentElementId = RevitElementExtension.GetBoundarySegmentElementId(boundarySegment, out elementId);
						if (null != boundarySegmentElementId)
						{
							element = this.m_Doc.GetElement(boundarySegmentElementId);
						}
						if (null != elementId && element is RevitLinkInstance)
						{
							revitLinkInstance = (element as RevitLinkInstance);
							element = revitLinkInstance.GetLinkDocument().GetElement(elementId);
						}
						else
						{
							element = boundarySegment.GetElement(this.m_Doc);
						}
						list.Add(new CurveInformationBean(boundarySegment.GetCurve(), element, revitLinkInstance));
					}
					break;
				}
			}
			return list;
		}

		public Document m_Doc;
	}
}
